package com.xiao.activity;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.bokeyuan.R;
import com.xiao.blogs.fragment.FragmentBlogs;
import com.xiao.bokeyuan.utils.I;
import com.xiao.bokeyuan.utils.JudgeWifi;
import com.xiao.myself.slide.utils.QQSlideUtils;
import com.xiao.news.fragment.FragmentHotNews;
import com.xiao.news.fragment.FragmentLabel;
import com.xiao.news.fragment.FragmentOffine;
import com.xiao.slipUtils.D;
import com.xiao.slipUtils.SlideMenuUtils;

public class MainActivity extends Activity {

	FragmentTransaction mft;
	FragmentManager manager;
	SharedPreferences mShare;
	SharedPreferences mshareeye;
	//��ס����ģʽÿ�θ��ĵı���id

	int mIdBackgroud;
	public FragmentHotNews mFragmentHotNews;

	// ��ȡ��Դ
	Resources res;
	// ��ϵ������
	//�Զ��廮��QQ��������
	public QQSlideUtils<LinearLayout> QQutils;
	// ��߲���
	
	Context context;
	public RelativeLayout mRLlayout;
	RelativeLayout mLLleftlayout;
	LinearLayout  mllrightLayout;
	// ���������ӹ��¼���
	public SlideMenuUtils<LinearLayout> mSlideTouch;
	// ListView mlistView;
	// �������е���ƬFramelayout
	public RelativeLayout mfLayout;
	// �ұ߲˵�������
	
public FrameLayout mfram;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		context = this;
		// ��ʼ������
		initLayoutView();
		setListener();
		// ���ü�����
		// setListener();
		// ���ô����¼�
		setTouchListener();
		//�������ʱ  ������������
		startHotNews();
	}
	/**
	 *  //�������ʱ  ������������
	 */
	private void startHotNews() {
		//���ж������Ƿ����
		boolean fo=JudgeWifi.isAvailable(this);
		if(fo==false){
			Toast.makeText(this, "����ʧ��", 2000).show();
		}else{
			// TODO Auto-generated method stub
			String mtitle = res.getString(R.string.app_name);
			// ��ȡ��Ӧ�ı���
			String title = res.getString(R.string.menu_hots_news);
			// ���ñ���
			MainActivity.this.setTitle(mtitle + "-" + title);
			manager = getFragmentManager();
			mft = manager.beginTransaction();
			// ����fragment����
			Fragment fragment = new FragmentHotNews();
			Bundle b = new Bundle();
			b.putString(D.NEWS_URL, I.URL_HOST_NEWS);
			fragment.setArguments(b);
			mft.replace(R.id.fragment_main, fragment);
			mft.addToBackStack(null);
			mft.commit();
		}
		
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if(event.getKeyCode()==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_UP){
			if(mFragmentHotNews!=null){
				manager.popBackStack();
				mFragmentHotNews=null;
				
				return true;
			}
		}
		return super.dispatchKeyEvent(event);
	}
	private void setTouchListener() {
		// TODO Auto-generated method stub
		setListViewTouchListener();

	}

	/**
	 * ���ݴ����¼�
	 */
	public  void setListViewTouchListener() {
		QQutils=new QQSlideUtils<LinearLayout>(this, mLLleftlayout, mllrightLayout, mRLlayout, mfLayout);
		//		mSlideTouch = new SlideMenuUtils<LinearLayout>(this, mLLlayout,
		//				mllrightLayout, mRLlayout, mfLayout);
	}

	/**
	 * ���ô����¼�����������relativeLayout�����¼��ӹ���
	 */
	//	private void setRllayoutTouchListener() {
	//		mRLlayout.setOnTouchListener(new OnTouchListener() {
	//
	//			@Override
	//			public boolean onTouch(View v, MotionEvent event) {
	//				// TODO Auto-generated method stub
	//				return mslideUtils.onTouch(v, event);
	//			}
	//		});
	//	}

	private void initLayoutView() {
		mShare=getSharedPreferences(D.ID_IMAGE, MODE_PRIVATE);
		mshareeye=getSharedPreferences(D.HELPEYE, MODE_PRIVATE);
		
		mRLlayout = (RelativeLayout) findViewById(R.id.rllayout);
		mLLleftlayout = (RelativeLayout) findViewById(R.id.llLeftlayout);
		mllrightLayout = (LinearLayout) findViewById(R.id.Rightlayout);
		// mlistView=(ListView) findViewById(R.id.listview);
		mfLayout = (RelativeLayout) findViewById(R.id.fragment_main);
		//���±�ռλ��
		mfram=(FrameLayout) findViewById(R.id.fl_details);
		//		mfLayout.setClickable(true);
		mLLleftlayout.setClickable(false);
		//		mLLleftlayout.setClickable(false);
		mLLleftlayout.setFocusable(false);
		//ȡ�������ﱣ���������
		int imageid = mShare.getInt(D.ID_IMAGE, R.drawable.bg);
		//���imageid !=-1  ��˵���й�ѡ  �򽫱�������Ϊ��Ӧ�Ĺ�ѡֵ
		if(imageid!=-1){
			
			mRLlayout.setBackgroundResource(imageid);
		}else{
			mRLlayout.setBackgroundResource(R.drawable.bg);
		}
		
		//ȡ������ģʽ
		boolean eye=mshareeye.getBoolean(D.HELPEYE, false);
		if(eye==true){
			mfLayout.setBackgroundResource(R.drawable.helpeye);
		}
	}


	/**
	 * ע����ʾ/���ز˵��Ĺ㲥
	 */

	/**
	 * ��ʼ������
	 */
	private void setListener() {
		// �õ���Դ����
		res = getResources();

		// �������Ű�ť�����¼�
		setNewsListener(R.id.host_news, R.string.menu_hots_news,
				I.URL_HOST_NEWS);
		// �������Ű�ť�¼�
		setNewsListener(R.id.latest_news, R.string.menu_last_news,
				I.URL_LAST_NEWS);
		//�Ƽ����Ű�ť
		setNewsListener(R.id.recommend_news, R.string.menu_recommend_news,
				I.URL_RECOMMEND_NEWS);
		//��ǩ��ť�¼�
		//		setButtonListener(R.id.bookmark,R.string.menu_book_mark,);
		setLebelListenr();
		//���������ť
		offlineBrowse();
		//�˳���ť
		exist();
		//���ð�ť
		setSettingListener();
		//������ť
		setSearchListener();
		/*
		 * ���в��Ͱ�ť
		 */
		
		//���в��Ͱ�ť
		setBokeListener(R.id.all_blog, R.string.menu_all_blog,
				I.URL_ALL_BLOG);
		//48Сʱ�Ķ����а�ť
		setBokeListener(R.id.hours48Reading, R.string.menu_48hours_sort,
				I.URL_SORT_48HOURS_SORT);
		//10�����Ƽ����а�ť
		setBokeListener(R.id.day10recommend, R.string.menu_day10,
				I.URL_DAY10_RECOMMEND_SORT);
	}
	
	/**
	 * //�˳���ť
	 */
	private void exist() {
		findViewById(R.id.exit).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}
	/**
	 * 	//���������ť
	 */
	private void offlineBrowse() {
		findViewById(R.id.offline_browse).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				QQutils.hideLeftAndCenterMenu(0);
				QQutils.hideLeftMenuListener();
				MainActivity.this.setTitle("bokeyuan" + "-" + "�������");
				manager = getFragmentManager();
				mft = manager.beginTransaction();
				Fragment f=new FragmentOffine();
				//				mfLayout.setOnTouchListener((OnTouchListener) f);
				mft.replace(R.id.fragment_main, f);
				mft.commit();
			}
		});
	}
	private void setSearchListener() {
		findViewById(R.id.search).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(context, ActivitySearch.class);
			
			startActivity(intent);
			overridePendingTransition(R.anim.right_in, R.anim.left_out);
			}
		});
	}
	private void setSettingListener() {
		findViewById(R.id.setting).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//��������ģʽ����
				Intent intent=new Intent(MainActivity.this,ActivitySeting.class);
				//				overridePendingTransition(R.anim.right_ina, (Integer) null);

				startActivity(intent);
				overridePendingTransition(R.anim.right_in, R.anim.left_out);
			}
		});
	}

	private void setLebelListenr() {
		findViewById(R.id.bookmark).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				QQutils.hideLeftAndCenterMenu(0);
				QQutils.hideLeftMenuListener();
				// ���ñ���
				MainActivity.this.setTitle("bokeyuan" + "-" + "��ǩ");
				manager = getFragmentManager();
				mft = manager.beginTransaction();
				Fragment f=new FragmentLabel();
				//				mfLayout.setOnTouchListener((OnTouchListener) f);
				mft.replace(R.id.fragment_main, f);
				mft.commit();
			}
		});
	}

	/**
	 * �������Ű�ť�����¼� �����е����Ű�ť����һ��fragment��Ƭ
	 * 
	 * @param id
	 *            ��ť��id
	 * @param title1
	 *            ��ť��Ӧ�ı��� ��������������title1������������
	 * @param url
	 *            ÿ����ť��Ӧ����ַ
	 * 
	 */
	private void setNewsListener(int id, final int title1, final String url) {

		findViewById(id).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				int x=0;
				//				QQutils.hideLeftAndCenterMenu();
				//����߲˵�����
				QQutils.hideLeftAndCenterMenu(x);
				//                     int x=0;
				//				QQutils.hideLeftAndCenterMenu(x);
				String mtitle = res.getString(R.string.app_name);
				// ��ȡ��Ӧ�ı���
				String title = res.getString(title1);
				// ���ñ���
				MainActivity.this.setTitle(mtitle + "-" + title);
				manager = getFragmentManager();
				mft = manager.beginTransaction();
				// ����fragment����
				Fragment fragment = new FragmentHotNews();
				Bundle b = new Bundle();
				b.putString(D.NEWS_URL, url);
				fragment.setArguments(b);
				mft.replace(R.id.fragment_main, fragment);
				
				mft.commit();

			}
		});

	}
	private void setBokeListener(int id, final int title1, final String url) {

		findViewById(id).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				int x=0;
				//				QQutils.hideLeftAndCenterMenu();
				//����߲˵�����
				QQutils.hideLeftAndCenterMenu(x);
				//                     int x=0;
				//				QQutils.hideLeftAndCenterMenu(x);
				String mtitle = res.getString(R.string.app_name);
				// ��ȡ��Ӧ�ı���
				String title = res.getString(title1);
				// ���ñ���
				MainActivity.this.setTitle(mtitle + "-" + title);
				manager = getFragmentManager();
				mft = manager.beginTransaction();
				// ����fragment����
				Fragment fragment = new FragmentBlogs();
				Bundle b = new Bundle();
				b.putString(D.NEWS_URL, url);
				fragment.setArguments(b);
				mft.replace(R.id.fragment_main, fragment);
				mft.commit();

			}
		});

	}
	public void setBackGroudColor(int id){
		//����һ��activity���ô˷���ʱ  ��Ҫ���µ���mShare����
		mShare=getSharedPreferences(D.ID_IMAGE,MODE_PRIVATE);
		Editor edit = mShare.edit();
		mRLlayout.setBackgroundResource(id);
		mIdBackgroud=id;
		//���Ƴ����������  Ȼ���ٱ���
		edit.putInt(D.ID_IMAGE,id);
		edit.commit();
	}


@Override
protected void onStart() {
	// TODO Auto-generated method stub
	super.onStart();
	int imageid = mShare.getInt(D.ID_IMAGE, R.drawable.bg);
	if(imageid!=-1){
		
		mRLlayout.setBackgroundResource(imageid);
	}else{
		mRLlayout.setBackgroundResource(R.drawable.bg);
	}
	
	//ȡ������ģʽ
	boolean eye=mshareeye.getBoolean(D.HELPEYE, false);
	if(eye==true){
		mfLayout.setBackgroundResource(R.drawable.helpeye);
	}else{
		mfLayout.setBackgroundResource(R.drawable.bg);
	}
}





//dispa
//		long mExitTime;//��¼��back����ʱ�䣬��λ������
//		@Override
//		public boolean dispatchKeyEvent(KeyEvent event) {
//			//��ȡ������
//			int keyCode=event.getKeyCode();
//			if(keyCode==event.KEYCODE_BACK&&event.getAction()==MotionEvent.ACTION_UP){
//				if(!D.LeftMenu.isShow){//�����˵�δ��ʾ������ʾ���˵�
//					mSlideTouch.showLeftMenuListener();
//				}else{//�����˵�����ʾ
//					if(System.currentTimeMillis()-mExitTime>2000){//��ǰʱ������һ�ΰ����¼�����2��
//						//���水�µ�ʱ��
//						mExitTime=System.currentTimeMillis();
//						Toast.makeText(MainActivity.this, "�ٰ�һ���˳�", 1).show();
//					}else{					
//						finish();//�ر�MainActivity
//					}
//				}
//			}
//			
//			return true;
//		}
}
